package com.dianrui.agent.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.androidkun.xtablayout.XTabLayout;
import com.dianrui.agent.R;
import com.dianrui.agent.activity.SearchOrderActivity;
import com.dianrui.agent.adapter.OrderListPageAdapter;
import com.dianrui.agent.event.OrderRedDotData;
import com.dianrui.agent.util.MyUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;


import java.util.List;

import butterknife.BindView;


public class FragmentMainOrder extends BaseFragment {
    @BindView(R.id.xTablayout)
    XTabLayout tablayout;
    @BindView(R.id.pager)
    ViewPager pager;
    @BindView(R.id.search_order_img)
    ImageView searchImg;
    private OrderListPageAdapter adapter;


    @Override
    public int getContentViewId() {
        return R.layout.fragment_main_order;
    }

    @Override
    public void initView() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        FragmentManager supportFragmentManager =getChildFragmentManager();
        adapter = new OrderListPageAdapter(supportFragmentManager,getActivity());
        pager.setAdapter(adapter);
        tablayout.setupWithViewPager(pager);
        pager.setOffscreenPageLimit(4);
        for (int i = 0; i < tablayout.getTabCount(); i++) {
            XTabLayout.Tab tab = tablayout.getTabAt(i);
            if (tab != null) {
                tab.setCustomView(adapter.getTabView(i));
            }
        }
        searchImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(),SearchOrderActivity.class));
            }
        });
    }



    //我的订单上面的红点
    @SuppressLint("SetTextI18n")
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(OrderRedDotData redDotData) {
        List<Integer> numberList = redDotData.ordernumberList;
        for (int i = 0; i < tablayout.getTabCount(); i++) {
            XTabLayout.Tab tab = tablayout.getTabAt(i);
            TextView hasNewMsg1 = tab.getCustomView().findViewById(R.id.tab_msg_num1);
            if (0== i) {
                if (numberList.get(0)>9999) {
                    hasNewMsg1.setVisibility(View.VISIBLE);
                    hasNewMsg1.setText("9999+");
                }else if (numberList.get(0)<=0) {
                    hasNewMsg1.setVisibility(View.GONE);
                }else {
                    hasNewMsg1.setText(redDotData.ordernumberList.get(0)+" ");
                    hasNewMsg1.setVisibility(View.VISIBLE);
                }
            }

            if (1== i) {
                if (numberList.get(1)>9999) {
                    hasNewMsg1.setVisibility(View.VISIBLE);
                    hasNewMsg1.setText("9999+");
                }else if (numberList.get(1)<=0) {
                    hasNewMsg1.setVisibility(View.GONE);
                }else {
                    hasNewMsg1.setText(redDotData.ordernumberList.get(1)+" ");
                    hasNewMsg1.setVisibility(View.VISIBLE);
                }
            }
            if (2== i) {
                if (numberList.get(2)>99) {
                    hasNewMsg1.setVisibility(View.VISIBLE);
                    hasNewMsg1.setText("99+");
                }else if (numberList.get(2)<=0) {
                    hasNewMsg1.setVisibility(View.GONE);
                }else {
                    hasNewMsg1.setText(redDotData.ordernumberList.get(2)+" ");
                    hasNewMsg1.setVisibility(View.VISIBLE);
                }
            }
            if (3 == i) {
                if (numberList.get(3)>9999) {
                    hasNewMsg1.setVisibility(View.VISIBLE);
                    hasNewMsg1.setText("9999+");
                }else if (numberList.get(3)<=0) {
                    hasNewMsg1.setVisibility(View.GONE);
                }else {
                    hasNewMsg1.setText(redDotData.ordernumberList.get(3)+" ");
                    hasNewMsg1.setVisibility(View.VISIBLE);
                }
            }

        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
